package com.wq.article.service;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.UpdateWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.wq.article.entity.Article;
import com.wq.article.mapper.ArticleMapper;
import com.wq.common.util.IdWorker;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import java.util.List;
import java.util.Map;
import java.util.Set;

@Service
public class ArticleService {
    @Autowired
    private ArticleMapper articleMapper;
    @Autowired
    private IdWorker idWorker;

    /**
     * 根据id查询Label
     *
     * @param id
     * @return
     */
    public Article queryById(String id) {
        return articleMapper.selectById(id);
    }

    /**
     * 查询所有
     *
     * @return
     */
    public List<Article> findAll() {
        return articleMapper.selectList(null);
    }

    /**
     * 新增
     *
     */
    public void save(Article article) {
        article.setId(idWorker.nextId() + "");
        articleMapper.insert(article);
    }

    /**
     * 更新
     *
     */
    public void update(Article article) {
        articleMapper.updateById(article);
    }

    /**
     * 根据id删除
     *
     * @param id
     */
    public void delete(String id) {
        articleMapper.deleteById(id);
    }

    //添加文章
    public void add(Article article){
        article.setId(idWorker.nextId()+"");
        articleMapper.insert(article);
    }
    //修改文章
    public void update1(Article article){
       UpdateWrapper<Article> wrapper=new UpdateWrapper<Article>();
       wrapper.eq("id",article.getId());
       articleMapper.update(article,wrapper);
    }
    //审核状态
    public void checkById(String state, String id){
        Article article=new Article();
        article.setState(state);
        article.setId(id);
        articleMapper.updateById(article);
    }
    public Page search(Map map, int page, int size) {
        Set<String> set = map.keySet();
        QueryWrapper<Article> queryWrapper = new QueryWrapper<>();
        for (String s : set) {
            queryWrapper.eq(null != map.get(s), s, map.get(s));
        }
        Page<Article> articlePage = new Page<>();
        return articleMapper.selectPage(articlePage, queryWrapper);
    }



}
